import { Controller } from "../../mvc/core/Controller";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { IView } from "../../mvc/interfaces/IView";
import { PetMingModule } from "../../../modules/PetMingModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { PetMingExchangeWindow } from "../view/pet/ming/PetMingExchangeWindow";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { PetMingJuWindow } from "../view/pet/ming/PetMingJuWindow";
import { GameProxy } from "../mvc/GameProxy";
import { EPlayerItemPosType } from "../../../Message/Public/EPlayerItemPosType";
import { ItemData } from "../resource/info/ItemData";
import { SActiveToClient } from "../../../Message/Public/SActiveToClient";
import { ClockManager } from "../manager/ClockManager";
import { MergeRechargeData } from "../view/consumeCount/MergeRechargeData";
import { EActiveType } from "../../../Message/Public/EActiveType";
import { IPetMingModule } from "../../../modules/interfaces/IPetMingModule";
type int = number;
//class PetMingController
    
    export  class PetMingController extends Controller
    {
        constructor()
        {
            
            super();Dispatcher.addEventListener(EventName.PetMingMoudleOpen, this.onPetMingMoudleOpenHandler);
            Dispatcher.addEventListener(EventName.PetMingExchangeWindowOpen, this.onPetMingExchangeWindowOpenHandler);
            Dispatcher.addEventListener(EventName.PetMingJuWindowOpen, this.onPetMingJuWindowOpenHandler);
            Dispatcher.addEventListener(EventName.PetMingEat, this.onPetMingEatHandler);
            Dispatcher.addEventListener(EventName.PetMingLock, this.onPetMingLockHandler);
            Dispatcher.addEventListener(EventName.PetMingDress, this.onPetMingDressHandler);
            Dispatcher.addEventListener(EventName.PetMingUnDress, this.onPetMingUnDressHandler);
            Dispatcher.addEventListener(EventName.PetMingUpgrade, this.onPetMingUpgradeHandler);
            Dispatcher.addEventListener(EventName.PetMingOneKeyJu, this.onPetMingOneKeyJuHandler);
            Dispatcher.addEventListener(EventName.PetMingOneKeyGet, this.onPetMingOneKeyGetHandler);
            Dispatcher.addEventListener(EventName.PetMingOneKeyJuStart, this.onPetMingOneKeyJuStartHandler);
            Dispatcher.addEventListener(EventName.PetMingGoldJuTimeUpdate, this.onPetMingGoldJuTimeUpdateHandler);
            return;
        }

        /* internal  */onGuidePetLifeStyleGetHandler(arg1: DataEvent): void
        {
            if (this.isJuShenOpen) 
            {
                this._juWindow.guideLifeStyleGet(int(arg1.data));
            }
            return;
        }

        /* internal  */onGuidePetLifeStyleGetCloseHandler(arg1: DataEvent): void
        {
            if (this.isJuShenOpen) 
            {
                this._juWindow.guideLifeStyleClose();
            }
            return;
        }

        /* internal  */onGuidePetLifeDressOptHandler(arg1: DataEvent): void
        {
            if (isViewShow) 
            {
                this._mingModule.guideDress();
            }
            return;
        }

        protected /* override */ initServer(): void
        {
            super.initServer();
            NetDispatcher.addCmdListener(ServerCommand.PetMingPackCommand, this.onPetMingPackCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetMingSwallExpCommand, this.onPetMingSwallExpCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetEquipItemInfoChange, this.onPetEquipItemInfoChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetMingJuPackCommand, this.onPetMingJuPackCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetMingHuntLevelCommand, this.onPetMingHuntLevelCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.onMoneyUpdateUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetMingMingliUpdate, this.onPetMingMingliUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetMingBrokenSoulUpdate, this.onPetMingBrokenSoulUpdateHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.onPackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.onPackChangeHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetMingCapacityCommand, this.onPetMingCapacityCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetMingJuGetItemCommand, this.onPetMingJuGetItemCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.PetMingHuntLevelDetailCommand, this.onPetMingHuntLevelDetailCommandHandler);
            NetDispatcher.addCmdListener(ServerCommand.ActiveListChange, this.onActiveListChangeHandler);
            Dispatcher.addEventListener(EventName.Guide_PetLifeStyleOpt, this.onGuidePetLifeStyleOptHandler);
            Dispatcher.addEventListener(EventName.Guide_PetLifeStyleGetOpt, this.onGuidePetLifeStyleGetHandler);
            Dispatcher.addEventListener(EventName.Guide_PetLifeStyleGetClose, this.onGuidePetLifeStyleGetCloseHandler);
            Dispatcher.addEventListener(EventName.Guide_PetLifeDressOpt, this.onGuidePetLifeDressOptHandler);
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._mingModule) 
            {
                this._mingModule = new PetMingModule();
                this._mingModule.addEventListener(WindowEvent.SHOW, this.onMingModuleShowHandler);
                this._mingModule.addEventListener(WindowEvent.CLOSE, this.onMingModuleCloseHandler);
            }
            return this._mingModule;
        }

        public get isJuShenOpen(): Boolean
        {
            if (this._juWindow && !this._juWindow.isHide) 
            {
                return true;
            }
            return false;
        }

        /* internal  */onJuShenWinShowHandler(arg1: WindowEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeStyleGetOpenHandler));
            return;
        }

        /* internal  */onJuShenWinCloseHandler(arg1: WindowEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeStyleGetCloseHandler));
            return;
        }

        /* internal  */onMingModuleShowHandler(arg1: WindowEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeDressOpenHandler));
            return;
        }

        /* internal  */onMingModuleCloseHandler(arg1: WindowEvent): void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.Guide_PetLifeDressCloseHandler));
            return;
        }

        /* internal  */onPetMingMoudleOpenHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data as string;
            view.show();
            this._mingModule.selectByUid(loc1);
            return;
        }

        /* internal  */onPetMingExchangeWindowOpenHandler(arg1: DataEvent): void
        {
            if (!this._mingExchangeWindow) 
            {
                this._mingExchangeWindow = new PetMingExchangeWindow();
            }
            this._mingExchangeWindow.show();
            return;
        }

        /* internal  */onPetMingJuWindowOpenHandler(arg1: DataEvent): void
        {
            if (!cache.petMing.isOpenJu) 
            {
                MsgManager.showRollTipsMsg(Language.getString(54786));
                return;
            }
            if (!this._juWindow) 
            {
                this._juWindow = new PetMingJuWindow();
                this._juWindow.addEventListener(WindowEvent.SHOW, this.onJuShenWinShowHandler);
                this._juWindow.addEventListener(WindowEvent.CLOSE, this.onJuShenWinCloseHandler);
                cache.petMing.juWindow = this._juWindow;
            }
            this._juWindow.updateGoldJuTime(cache.petMing.goldJuTime);
            this._juWindow.show();
            return;
        }

        /* internal  */onPetMingLockHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["lockFlag"] as Boolean;
            var loc2=arg1.data["items"] as Array<any>;
            var loc3=arg1.data["callBackFun"] as Function;
            GameProxy.petEquip.lock(loc1, loc2, loc3);
            return;
        }

        /* internal  */onPetMingEatHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["fromPosType"] as int;
            var loc2=arg1.data["fromPetUid"] as string;
            var loc3=arg1.data["itemDict"] as Map<any, any> /* flash.utils.Dictionary */;
            var loc4=arg1.data["callBackFun"] as Function;
            var loc5=arg1.data["exp"] as int;
            GameProxy.petEquip.swallowLifeStype(loc1, loc2, loc3, loc4, loc5);
            return;
        }

        /* internal  */onPetMingDressHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["petUid"] as string;
            var loc2=arg1.data["itemUid"] as string;
            var loc3=arg1.data["index"] as int;
            GameProxy.petEquip.dressLifeStype(loc1, loc2, loc3);
            this._mingModule.guideClose();
            return;
        }

        /* internal  */onPetMingUnDressHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["petUid"] as string;
            var loc2=arg1.data["bagUid"] as string;
            if (loc2 == null) 
            {
                loc2 = "";
            }
            GameProxy.petEquip.unDressLifeStype(loc1, loc2);
            return;
        }

        /* internal  */onPetMingUpgradeHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["petUid"] as string;
            var loc2=arg1.data["itemUid"] as string;
            GameProxy.petEquip.lifeStyleUpgrade(loc1, loc2);
            return;
        }

        /* internal  */onPetMingOneKeyJuHandler(arg1: DataEvent): void
        {
            var loc1=arg1.data["level"] as int;
            var loc2=arg1.data["type"] as int;
            GameProxy.petEquip.huntLifeStyle(loc1, loc2);
            cache.petMing.currentJuLevel = loc1;
            cache.petMing.isAfterJu = true;
            cache.petMing.isOnekeyJu = loc1 == 0;
            return;
        }

        /* internal  */onPetMingOneKeyGetHandler(arg1: DataEvent): void
        {
            var e: DataEvent;
            var fromPosType: int;
            var toPosType: int;
            var itemUidDict: Map<any, any> /* flash.utils.Dictionary */;
            var callBackFun: Function;
            var oneKeyGetSuccess: Function;

            var loc1;
            fromPosType = 0;
            itemUidDict = null;
            callBackFun = null;
            oneKeyGetSuccess = null;
            e = arg1;
            oneKeyGetSuccess = (): void
            {
                var loc1=null;
                var loc2=0;
                if (fromPosType == EPlayerItemPosType._EPlayerItemPosTypePetLifeStypeHunt) 
                {
                    var loc3=0;
                    var loc4=itemUidDict;
                    for (loc1 in loc4) 
                    {
                        loc2 = cache.pack.petMingJuPackCache.getIndexByUid(loc1);
                        cache.pack.petMingJuPackCache.moveBagOut(loc2);
                    }
                    NetDispatcher.dispatchCmd(ServerCommand.PetMingJuPackCommand, null);
                }
                if (callBackFun != null) 
                {
                    callBackFun(itemUidDict);
                }
                return;
            }
            fromPosType = e.data["fromPosType"] as int;
            toPosType = e.data["toPosType"] as int;
            itemUidDict = e.data["itemUidDict"] as Map<any, any> /* flash.utils.Dictionary */;
            callBackFun = e.data["callBackFun"] as Function;
            GameProxy.packProxy.moveBag(fromPosType, toPosType, itemUidDict, oneKeyGetSuccess);
            return;
        }

        /* internal  */onPetMingPackCommandHandler(arg1: Object): void
        {
            if (this._mingModule && !this._mingModule.isHide) 
            {
                this._mingModule.refreshMingPack();
            }
            if (this._juWindow && !this._juWindow.isHide) 
            {
                this._juWindow.updateLifeStyleNum();
            }
            return;
        }

        /* internal  */onPetMingSwallExpCommandHandler(arg1: int): void
        {
            if (this._mingModule && !this._mingModule.isHide) 
            {
                this._mingModule.updateSwallExp(arg1);
            }
            if (this._juWindow && !this._juWindow.isHide) 
            {
                this._juWindow.updateSwallExp(arg1);
            }
            return;
        }

        /* internal  */onPetEquipItemInfoChangeHandler(arg1: Object): void
        {
            if (this._mingModule && !this._mingModule.isHide) 
            {
                this._mingModule.refreshPetMingEquip();
            }
            return;
        }

        /* internal  */onPetMingJuPackCommandHandler(arg1: Object): void
        {
            if (this._juWindow && !this._juWindow.isHide) 
            {
                this._juWindow.refreshJuPack(true);
            }
            return;
        }

        /* internal  */onPetMingHuntLevelCommandHandler(arg1: Object=null): void
        {
            if (this._juWindow && !this._juWindow.isHide) 
            {
                this._juWindow.updateByHuntLevelInfo();
            }
            return;
        }

        /* internal  */onMoneyUpdateUpdateHandler(arg1: Object): void
        {
            if (this._juWindow && !this._juWindow.isHide) 
            {
                this._juWindow.updateGold(cache.role.money.gold);
            }
            return;
        }

        /* internal  */onPetMingMingliUpdateHandler(arg1: Object): void
        {
            return;
        }

        /* internal  */onPetMingBrokenSoulUpdateHandler(arg1: int): void
        {
            var loc1=cache.role.money.brokenSoul;
            if (this._mingModule && !this._mingModule.isHide) 
            {
                this._mingModule.updateBrokenSoul(loc1);
            }
            if (this._juWindow && !this._juWindow.isHide) 
            {
                this._juWindow.updateBrokenSoul(loc1);
                this._juWindow.glowBrokenSoul(arg1);
            }
            if (this._mingExchangeWindow && !this._mingExchangeWindow.isHide) 
            {
                this._mingExchangeWindow.updateBrokenSoul(loc1);
            }
            return;
        }

        /* internal  */onPackChangeHandler(arg1: Object): void
        {
            if (this._juWindow && !this._juWindow.isHide) 
            {
                this._juWindow.updatePropNum();
            }
            return;
        }

        /* internal  */onPetMingCapacityCommandHandler(arg1: Map<any, any> /* flash.utils.Dictionary */): void
        {
            if (this._mingModule && !this._mingModule.isHide) 
            {
                this._mingModule.updateEquipItemEnable();
            }
            return;
        }

        /* internal  */onPetMingJuGetItemCommandHandler(arg1: ItemData): void
        {
            if (!(this._juWindow && !this._juWindow.isHide)) 
            {
            };
            return;
        }

        /* internal  */onPetMingOneKeyJuStartHandler(arg1: DataEvent): void
        {
            this.autoJuNextMax();
            return;
        }

        /* internal  */autoJuNextMax(): void
        {
            var loc1=cache.petMing.getMaxCanPropCallLevel();
            if (loc1 != 5) 
            {
                GameProxy.petEquip.huntLifeStyle(loc1, 0, this.onekeyJuSuccess);
            }
            return;
        }

        /* internal  */onekeyJuSuccess(arg1: int, arg2: int): void
        {
            flash.utils.setTimeout(this.autoJuNextMax, 0);
            return;
        }

        /* internal  */onPetMingGoldJuTimeUpdateHandler(arg1: DataEvent): void
        {
            if (this._juWindow) 
            {
                this._juWindow.updateGoldJuTime(cache.petMing.goldJuTime);
            }
            return;
        }

        /* internal  */onPetMingHuntLevelDetailCommandHandler(arg1: Array<any>): void
        {
            if (!(this._juWindow && !this._juWindow.isHide)) 
            {
            };
            return;
        }

        /* internal  */onActiveListChangeHandler(arg1: Object): void
        {
            var obj: Object;
            var active: SActiveToClient;
            var isActive: Boolean;
            var removeMergeRechargeActive: Function;

            var loc1;
            removeMergeRechargeActive = null;
            obj = arg1;
            removeMergeRechargeActive = (): void
            {
                cache.petMing.discountActiveOpen = false;
                ClockManager.instance.removeDateCall(MergeRechargeData.activeToClient.endDt);
                onPetMingHuntLevelCommandHandler();
                Dispatcher.dispatchEvent(new DataEvent(EventName.PetMingGoldJuTimeUpdate, cache.petMing.goldJuTime));
                return;
            }
            active = cache.active.getActiveByCode(EActiveType._EActiveTypeLifestyleGold);
            isActive = cache.active.isActiveInEffectByInfo(active);
            if (isActive) 
            {
                cache.petMing.discountActiveOpen = true;
                cache.petMing.discountActivePrice = active.modulus.split(",");
                ClockManager.instance.addDateCall(active.endDt, removeMergeRechargeActive);
            }
            else 
            {
                cache.petMing.discountActiveOpen = false;
            }
            return;
        }

        /* internal  */onGuidePetLifeStyleOptHandler(arg1: DataEvent): void
        {
            if (this.isJuShenOpen) 
            {
                this._juWindow.guideLifeStyleOpt(int(arg1.data));
            }
            return;
        }

        private /* var */_mingModule: IPetMingModule;

        private /* var */_mingExchangeWindow: PetMingExchangeWindow;

        private /* var */_juWindow: PetMingJuWindow;
    }
